const {
  getEventData
} = getApp()
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    title: {
      type: String,
      value: ''
    },

    value: {
      type: null,
      value: []
    },

    inset: Boolean,

    required: {
      type: Boolean,
      value: false
    },
    placeholder: {
      type: String,
      value: '请选择'
    },
    inset: {
      type: Boolean,
      value: false
    },
    valueKey: {
      type: String,
      value: ''
    },
    nodeKey: {
      type: String,
      value: ''
    }
  },

  data: {
    isShowPopup: false
  },
  methods: {
    handleClick() {
      this.triggerEvent('click');
    },

    handleDeleteAttachment(event) {
      const {
        index
      } = getEventData(event)
      this.triggerEvent('deletebutton', index)
    },

    handleShowDetail(event) {
      const {
        item
      } = getEventData(event)
      this.setData({
        currentItem: item,
        showDialog: true
      })
    },

    onCloseDetail() {
      this.setData({
        currentItem: null,
        showDialog: false
      })
    },

    onChange(event) {
      this.onClose();
      let value = event.detail;

      if (this.data.nodeKey) {
        value = value[this.data.nodeKey];
      }
      this.triggerEvent('change', value);
    },

    onRemove(event) {
      const {
        index
      } = getEventData(event);
      const data = this.data.value;
      data.splice(index, 1)
      this.setData({
        value: data
      })

      this.triggerEvent('change', this.data.value)
    },

    onNumChange(event) {
      const {
        index
      } = getEventData(event);
      const data = this.data.value;
      data[index].quantity = event.detail
      this.setData({
        value: data
      })

      this.triggerEvent('change', this.data.value)
    },
  }
})